﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MarketOnlineBUS;
using MarketOnlineDTO;

public partial class cms_ManageProduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["login"] == null)
        {
            Response.Redirect("Login.aspx");
        }
                
        LoadDanhsachSP();
        LoadDanhSachLoaiSP();
    }

    void LoadDanhsachSP()
    { 
        List<SanPhamDTO> list = new List<SanPhamDTO>();
        list = new SanPhamBUS().LayDanhSachSanPham();

        GridView1.DataSource = list;
        
        GridView1.DataBind();
        Dispose();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        LoadDanhsachSP();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int row = int.Parse(GridView1.Rows[Convert.ToInt32(e.RowIndex)].Cells[0].Text.ToString());
        new SanPhamBUS().XoaSanPham(row);
        LoadDanhsachSP();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int row = int.Parse(GridView1.Rows[e.NewEditIndex].Cells[0].Text.ToString());        
        string url = "ManageProductDetail.aspx?catid=" + row;
        Response.Redirect(url);
    }

    void LoadDanhSachLoaiSP()
    {
        List<LoaiSanPhamDTO> lst = new List<LoaiSanPhamDTO>();
        lst = new LoaiSanPhamBUS().LayDanhSachLoaiSanPham();
        ddlTimTheoLoai.DataSource = lst;
        ddlTimTheoLoai.DataValueField = "MaLoaiSP";
        ddlTimTheoLoai.DataTextField = "TenLoaiSP";        
        ddlTimTheoLoai.DataBind();
        Dispose();
    }

    protected void btnThem_Click(object sender, EventArgs e)
    {
        string url = "ManageProductDetail.aspx?catid=0";
        Response.Redirect(url);
    }


}